package org.elanfox.intellicommunity.intellicommunityadmin.mq;

import lombok.extern.slf4j.Slf4j;
import org.elanfox.intellicommunity.biz.dto.msg.MsgSaveDTO;
import org.elanfox.intellicommunity.biz.entity.equipment.CardPO;
import org.elanfox.intellicommunity.biz.entity.equipment.LocatorPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.CardService;
import org.elanfox.intellicommunity.biz.service.core.equipment.LocatorService;
import org.elanfox.intellicommunity.biz.service.core.msg.AlarmMsgService;
import org.elanfox.intellicommunity.biz.service.core.msg.LocationMsgService;
import org.elanfox.intellicommunity.common.ienum.MsgEnums;
import org.elanfox.intellicommunity.intellicommunityadmin.service.CmdReceiveService;
import org.elanfox.intellicommunity.intellicommunityadmin.ws.WsMsgHandler;
import org.elanfox.intellicommunity.msg.constant.MsgConstant;
import org.elanfox.intellicommunity.msg.dto.CmdDTO;
import org.elanfox.intellicommunity.msg.dto.RawTagDTO;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息接受
 *
 * @author allan
 * @version 1.0.0
 * @date 2019/12/17
 */
@Component
@Slf4j
public class MsgConsumer {
    @Autowired
    private LocationMsgService locationMsgService;

    @Autowired
    private AlarmMsgService alarmMsgService;
    @Autowired
    private WsMsgHandler wsMsgHandler;
    @Autowired
    private CardService cardService;
    @Autowired
    private CmdReceiveService cmdReceiveService;
    //    @Autowired
//    private BaseStationService baseStationService;
    @Autowired
    private LocatorService locatorService;

    /**
     * 接受定位消息
     *
     * @param msg
     */
    @RabbitListener(queues = MsgConstant.QUEUE_LOCATION, concurrency = "5-10")
    public void locationProcess(Map<String, Object> msg) {
        try {
            RawTagDTO msgDTO = (RawTagDTO) msg.get("msg");
            log.info("收到标签信息====>" + msgDTO.toString());
            //存db
            MsgSaveDTO msgSaveDTO = new MsgSaveDTO(msgDTO);
            if (msgSaveDTO.getCardId() == 1676948976) {
                log.info("收到1676948976=>" + msgSaveDTO.toString());
            }
            LocatorPO locatorPO = locatorService.findByNo(msgSaveDTO.getLocatorId());
            if (locatorPO != null && locatorPO.isAlarmedImmediately()) {
                CardPO cardPO = cardService.find(msgDTO.getTagId());
                //特殊住户，接收到直接报警
                if (cardPO != null && cardPO.cardEnabled() && cardPO.getUser().getFeature().getCode() > 0) {
                    wsMsgHandler.send(cardPO, locatorPO);
                    msgSaveDTO.setAlarmType(MsgEnums.AlarmType.SPECIAL_ALARM);
                    //处理
                    alarmMsgService.save(msgSaveDTO);
                }
            }

            locationMsgService.save(msgSaveDTO);
        } catch (Exception ex) {
            log.error(MsgConstant.QUEUE_LOCATION + "消费者收到消息异常--" + ex.getMessage(), ex);
        }
    }

    /**
     * 接受报警消息
     *
     * @param msg
     */
    @RabbitListener(queues = MsgConstant.QUEUE_ALARM)
    public void alarmProcess(Map<String, Object> msg) {
        try {
            RawTagDTO msgDTO = (RawTagDTO) msg.get("msg");

            //web socked通知
            CardPO cardPO = cardService.find(msgDTO.getTagId());

            if (cardPO != null && cardPO.cardEnabled()) {
                wsMsgHandler.send(cardPO, msgDTO, "");
                //处理
                if (msgDTO.isAlarmedButton()) {
                    MsgSaveDTO msgSaveDTO = new MsgSaveDTO(msgDTO);
                    //如果是低电压报警直接返回
                    if (msgSaveDTO.getAlarmType() == MsgEnums.AlarmType.BATTERY_ALARM) {
                        return;
                    }
                    alarmMsgService.save(msgSaveDTO);
                }
            }
        } catch (Exception ex) {
            log.error(MsgConstant.QUEUE_ALARM + "消费者收到消息异常--" + ex.getMessage(), ex);
        }
    }

    @RabbitListener(queues = MsgConstant.QUEUE_COMMAND_RESP)
    public void cmdProcess(Map<String, Object> msg) {
        CmdDTO cmdDTO = (CmdDTO) msg.get("msg");
        // TODO: 2020/3/20 响应的处理
        cmdReceiveService.receive(cmdDTO);

    }
}
